package com.zzw.controller;

import com.zzw.message.MessageConstant;
import com.zzw.pojo.Performance;
import com.zzw.service.PerformanceService;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/report")
public class ReportController {
    @Autowired
    private PerformanceService performanceService;

    @RequestMapping("/exportPerformanceReport")
    public void exportPerformanceReport(HttpServletRequest request, HttpServletResponse response) {
        ServletOutputStream outputStream = null;
        try {
            List<Performance> resultData = performanceService.getReportData();
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet(MessageConstant.REPORT_TITLE);
            HSSFRow row0 = sheet.createRow(0);
            row0.createCell(0).setCellValue(MessageConstant.REPORT_CELL0);
            row0.createCell(1).setCellValue(MessageConstant.REPORT_CELL1);
            row0.createCell(2).setCellValue(MessageConstant.REPORT_CELL2);
            row0.createCell(3).setCellValue(MessageConstant.REPORT_CELL3);
            row0.createCell(4).setCellValue(MessageConstant.REPORT_CELL4);
            //取出返回结果数据，准备将报表数据写入到Excel文件中
            for (int i = 0; i < resultData.size(); i++) {
                Performance performance = resultData.get(i);
                String name = performance.getName();
                Double firstPerformance = performance.getFirstPerformance();
                Double secondPerformance = performance.getSecondPerformance();
                Double thirdPerformance = performance.getThirdPerformance();
                Double fourthPerformance = performance.getFourthPerformance();
                HSSFRow row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(name);
                row.createCell(1).setCellValue(firstPerformance);
                row.createCell(2).setCellValue(secondPerformance);
                row.createCell(3).setCellValue(thirdPerformance);
                row.createCell(4).setCellValue(fourthPerformance);
            }
            outputStream = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");//代表的是Excel文件类型
            response.setHeader("content-Disposition", "attachment;filename=report.xlsx");//指定以附件形式进行下载
            workbook.write(outputStream);
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @RequestMapping("/exportOrderReport")
    public void exportOrderReport(HttpServletRequest request, HttpServletResponse response) {
        ServletOutputStream outputStream = null;
        try {
            List<Performance> resultData = performanceService.getReportData();
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet(MessageConstant.REPORT_TITLE);
            HSSFRow row0 = sheet.createRow(0);
            row0.createCell(0).setCellValue(MessageConstant.REPORT_CELL0);
            row0.createCell(1).setCellValue(MessageConstant.REPORT_CELL1);
            row0.createCell(2).setCellValue(MessageConstant.REPORT_CELL2);
            row0.createCell(3).setCellValue(MessageConstant.REPORT_CELL3);
            row0.createCell(4).setCellValue(MessageConstant.REPORT_CELL4);
            //取出返回结果数据，准备将报表数据写入到Excel文件中
            for (int i = 0; i < resultData.size(); i++) {
                Performance performance = resultData.get(i);
                String name = performance.getName();
                Double firstPerformance = performance.getFirstPerformance();
                Double secondPerformance = performance.getSecondPerformance();
                Double thirdPerformance = performance.getThirdPerformance();
                Double fourthPerformance = performance.getFourthPerformance();
                HSSFRow row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(name);
                row.createCell(1).setCellValue(firstPerformance);
                row.createCell(2).setCellValue(secondPerformance);
                row.createCell(3).setCellValue(thirdPerformance);
                row.createCell(4).setCellValue(fourthPerformance);
            }
            outputStream = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");//代表的是Excel文件类型
            response.setHeader("content-Disposition", "attachment;filename=report.xlsx");//指定以附件形式进行下载
            workbook.write(outputStream);
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
